package cjyyp.netty中futrue和promise接口;

import io.netty.channel.EventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;

@Slf4j
public class 使用netty中的future {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        NioEventLoopGroup group=new NioEventLoopGroup();
        EventLoop eventLoop = group.next();
        Future<Integer> nettyFuture = eventLoop.submit(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                Thread.sleep(1000);
                log.info("执行中。。。");
                return 20;
            }
        });
        //主动接受，get()方法会阻塞
        log.info("结果是[{}]",nettyFuture.get());
        //异步接收
        nettyFuture.addListener(new GenericFutureListener<Future<? super Integer>>() {
            @Override
            public void operationComplete(Future<? super Integer> future) throws Exception {
                log.info("结果是[{}]",future.getNow());//这里的getNow()一定可以拿到结果
            }
        });
        group.shutdownGracefully();
    }
}
